import Vue from "vue";
import VueRouter from "vue-router";
//引入仓库
import store from "../store";
//判断当前模块是否有该权限
function hasUrl(url,next){
  let result=store.getters.getUser.menus_url.some(item=>url===item)
  if(result){
    next()
  }else{
    next('/home')
  }
}

Vue.use(VueRouter);

export const routesArr = [
  {
    path: "/meuu",
    component: () => import("../views/meuu/meuu.vue"),
    meta: { title: "菜单管理" },
    beforeEnter(to, from, next) {
      hasUrl(to.path, next);
    },
  },
  {
    path: "/role",
    component: () => import("../views/role/role.vue"),
    meta: { title: "角色管理" },
    beforeEnter(to, from, next) {
      hasUrl(to.path, next);
    },
  },
  {
    path: "/con",
    component: () => import("../views/control/user.vue"),
    meta: { title: "管理员管理" },
    beforeEnter(to, from, next) {
      hasUrl(to.path, next);
    },
  },
  {
    path: "/goods",
    component: () => import("../views/goods/goods.vue"),
    meta: { title: "商品分类" },
    beforeEnter(to, from, next) {
      hasUrl(to.path, next);
    },
  },
  {
    path: "/specs",
    component: () => import("../views/specs/specs.vue"),
    meta: { title: "商品规格" },
    beforeEnter(to,from,next){
      hasUrl(to.path,next)
    }
  },{
    path:'/spgl',
    component:() => import("../views/spgl/spgl.vue"),
    meta: {title:"商品管理"},
    beforeEnter(to,from,next){
      hasUrl(to.path,next)
    }
  },{
    path:'/member',
    component:()=> import("../views/member/member.vue"),
    meta:{title:"会员管理"},
    beforeEnter(to,from,next){
      hasUrl(to.path,next)
    }
  },{
    //banneradd
    path:'/banner',
    component:()=> import("../views/banner/banner.vue"),
    meta:{title:"轮播图管理"},
    beforeEnter(to,from,next){
      hasUrl(to.path,next)
    }
  },
  {
    // secklist
    path:'/seck',
    component:()=> import('../views/seck/seck.vue'),
    meta:{title:"秒杀活动"},
    beforeEnter(to,from,next){
      hasUrl(to.path,next)
    }
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      //一级路由重定向
      path: "*",
      redirect: "/index",
    },
    {
      path: "/login",
      component: () => import("../pages/login.vue"),
    },
    {
      path: "/index",
      component: () => import("../pages/index.vue"),
      children: [
        {
          path: "",
          redirect: "/home",
        },
        {
          path: "/home",
          component: () => import("../views/home.vue"),
        },
        ...routesArr
      ],
    },
  ],
});

// //封装全局登录事件
router.beforeEach((to, from, next) => {
  // 1、先检测用户访问的地址是否是登录
  if (to.path == "/login") {
    next();
    return;
  }
  //2、判断用户是否登录
  if (store.getters.getUser) {
    next();
    return;
  }
  //3、如果未登录 就强制跳转到登录
  next("/login");
});

export default router;
